package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youdianda.context.BaseContext;
import com.youdianda.entity.*;
import com.youdianda.mapper.*;
import com.youdianda.result.Result;
import com.youdianda.service.IndexService;
import com.youdianda.vo.ArticleVo;
import com.youdianda.vo.ConfigFrontVo;
import com.youdianda.vo.YddArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysConfigMapper sysConfigMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysDictMapper sysDictMapper;

    @Autowired
    YddCateMapper yddCateMapper;

    @Autowired
    YddLinkMapper yddLinkMapper;

    @Autowired
    YddTagMapper yddTagMapper;

    @Autowired
    YddAdvimgMapper yddAdvimgMapper;

    @Autowired
    YddArticleMapper yddArticleMapper;

    @Autowired
    YddUserDataMapper yddUserDataMapper;
    @Autowired
    YddUserMapper yddUserMapper;
    @Autowired
    YddAdvposMapper yddAdvposMapper;

    /**
     * 用户端获取首页
     *
     * @return
     */
    @Override
    public Result index() {
        Map map = new HashMap();
        List<YddCate> yddCates = yddCateMapper.selectList(null);
        map.put("allCate", yddCates);
        List<YddLink> yddLinks = yddLinkMapper.selectList(null);
        map.put("allLink", yddLinks);
        List<YddTag> yddTags = yddTagMapper.selectList(null);
        map.put("allTag", yddTags);
        LambdaQueryWrapper<YddAdvimg> lqw1 = new LambdaQueryWrapper();
        lqw1.eq(YddAdvimg::getAdvimgpos, 15);
        List<YddAdvimg> banner = yddAdvimgMapper.selectList(lqw1);
        map.put("banner", banner);
        LambdaQueryWrapper<YddAdvimg> lqw2 = new LambdaQueryWrapper();
        lqw2.in(YddAdvimg::getAdvimgpos, 12, 13);
        List<YddAdvimg> showbanner = yddAdvimgMapper.selectList(lqw2);
        map.put("showBanner", showbanner);
        LambdaQueryWrapper<YddAdvimg> lqw3 = new LambdaQueryWrapper();
        lqw3.eq(YddAdvimg::getAdvimgpos, 14);
        List<YddAdvimg> sideBanner = yddAdvimgMapper.selectList(lqw3);
        map.put("sideBanner", sideBanner);

        Map config = new HashMap();
        List<SysConfig> sysConfigs = sysConfigMapper.selectList(null);
        config.put("allData", sysConfigs);
        LambdaQueryWrapper<SysConfig> lqw4 = new LambdaQueryWrapper();
        lqw4.groupBy(SysConfig::getTabValue);
        lqw4.select(SysConfig::getTabValue);
        List<SysConfig> sysConfigs1 = sysConfigMapper.selectList(lqw4);
        List<ConfigFrontVo> list = new ArrayList<>();
        sysConfigs1.forEach(s -> {
            //获取
            LambdaQueryWrapper<SysConfig> lqw5 = new LambdaQueryWrapper();
            lqw5.eq(SysConfig::getTabValue, s.getTabValue());
            lqw5.groupBy(SysConfig::getGroupValue);
            lqw5.select(SysConfig::getGroupValue);
            List<Map<String, Object>> maps = sysConfigMapper.selectMaps(lqw5);
            ConfigFrontVo configVo = new ConfigFrontVo();
            List<Map> children = new ArrayList<>();
            maps.forEach(s2 -> {
                Map sysConfigs2 = new HashMap();
                String name = String.valueOf(s2.get("group_value"));
                sysConfigs2.put("name", name);
                LambdaQueryWrapper<SysConfig> lqw6 = new LambdaQueryWrapper();
                lqw6.eq(SysConfig::getTabValue, s.getTabValue());
                lqw6.eq(SysConfig::getGroupValue, name);
                List<SysConfig> sysConfigs3 = sysConfigMapper.selectList(lqw6);
                sysConfigs2.put("data", sysConfigs3);
                children.add(sysConfigs2);
            });
            configVo.setChildren(children);
            configVo.setKey(s.getTabValue());
            if (s.getTabValue().equals("system")) {
                configVo.setName("系统设置");
            } else if (s.getTabValue().equals("base")) {
                configVo.setName("基础设置");
            } else {
                configVo.setName("其他设置");
            }
            list.add(configVo);
        });
        config.put("list", list);
        map.put("config", config);
        return Result.success(map);
    }


    /**
     * 获取文章详情
     *
     * @param id
     * @return
     */
    @Override
    public Result show(Integer id) {
        QueryWrapper<YddArticle> qw1 = new QueryWrapper();
        qw1.lt("id", id).select("MAX(id) as id");
        YddArticle prev = yddArticleMapper.selectOne(qw1);
        YddArticle yddArticle2 = new YddArticle();
        if (prev != null) {
            yddArticle2 = yddArticleMapper.selectById(prev.getId());
        }
        QueryWrapper<YddArticle> qw2 = new QueryWrapper();
        qw2.gt("id", id).select("min(id) as id");
        YddArticle next = yddArticleMapper.selectOne(qw2);
        YddArticle yddArticle3 = new YddArticle();
        if (next != null) {
            yddArticle3 = yddArticleMapper.selectById(next.getId());
        }
        Object data = recommend().getData();
        Map map = new HashMap();
        List nextList = new ArrayList();
        nextList.add(yddArticle2);
        List prevList = new ArrayList();
        prevList.add(yddArticle3);
        map.put("next", nextList);
        map.put("prev", prevList);
        map.put("recommend", data);

        YddArticle yddArticle = yddArticleMapper.selectById(id);
        YddArticleVo yddArticleVo = new YddArticleVo();
        BeanUtils.copyProperties(yddArticle, yddArticleVo);
        LambdaQueryWrapper<YddCate> qw3 = new LambdaQueryWrapper();
        qw3.select(YddCate::getCatename);
        qw3.eq(YddCate::getId, yddArticle.getCateid());
        YddCate yddCate = yddCateMapper.selectOne(qw3);
        yddArticleVo.setCatename(yddCate.getCatename());

        String currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<YddUserData> qw4 = new LambdaQueryWrapper();
        qw4.eq(YddUserData::getUserId, currentId);
        qw4.eq(YddUserData::getArticleId, id);
        List<YddUserData> yddUserData = yddUserDataMapper.selectList(qw4);
        if (yddUserData.size() != 0) {
            yddUserData.forEach(s -> {
                if (s.getType() == 1) {
                    yddArticleVo.setCollect(true);
                } else {
                    yddArticleVo.setLike(true);
                }
            });

        }
        map.put("info", yddArticleVo);
        return Result.success(map);
    }

    /**
     * 侧边推荐文章
     *
     * @return
     */
    @Override
    public Result recommend() {
        List<ArticleVo> recommend = yddArticleMapper.recommend();
        Map map = new HashMap();
        map.put("desc", "侧边最新文章");
        map.put("list", recommend);
        return Result.success(map);
    }
}
